package com.ecnu.web.controller.project;

import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.system.domain.ProjectFile;
import com.ecnu.system.domain.ProjectResult;
import com.ecnu.system.domain.dto.ProjectResultDTO;
import com.ecnu.system.service.IProjectResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "项目成果管理")
@RestController
@RequestMapping("/project/result")
public class ProjectResultController extends BaseController {
    @Autowired
    private IProjectResultService projectResultService;

    @ApiOperation(value = "成果列表")
    @PostMapping("/list")
    @PreAuthorize("@ss.hasPermi('project:result:list')")
    public TableDataInfo getList(@Valid @RequestBody ProjectResultDTO dto) {
        LoginUser loginUser = getLoginUser();
        return projectResultService.getList(dto,loginUser);
    }

    @ApiOperation(value = "成果列表-后台")
    @PostMapping("/adminList")
    @PreAuthorize("@ss.hasPermi('project:result:adminList')")
    public TableDataInfo adminList(@Valid @RequestBody ProjectResultDTO dto) {
        return projectResultService.adminList(dto);
    }

    @ApiOperation(value = "成果删除")
    @PostMapping("/del")
//    @PreAuthorize("@ss.hasPermi('project:result:del')")
    public AjaxResult del(@RequestBody String[] ids) {
        List<ProjectResult> projects = Arrays.stream(ids)
                .map(id -> {
                    ProjectResult file = new ProjectResult();
                    file.setId(id);
                    file.setDelFlag("2");
                    return file;
                })
                .collect(Collectors.toList());
        return projectResultService.updateBatchById(projects) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @ApiOperation(value = "成果提交")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('project:result:save')")
    public AjaxResult submit(@Valid @RequestBody ProjectResult projectResult) {
        LoginUser loginUser = getLoginUser();
        return projectResultService.save2(projectResult, loginUser);
    }

    @ApiOperation(value = "成果提交2")
    @PostMapping("/resultSubmit")
//    @PreAuthorize("@ss.hasPermi('project:result:resultSubmit')")
    public AjaxResult resultSubmit(@Valid @RequestBody ProjectResult projectResult) {
        LoginUser loginUser = getLoginUser();
        return projectResultService.save2(projectResult, loginUser);
    }
}
